package com.dongnam.quanly.common.controller;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.dongnam.quanly.common.dao.ChiTietNhapXuatDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.ChiTietNhapXuat;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class ChiTietNhapXuatController extends GenericController<ChiTietNhapXuat, String> {

	public ChiTietNhapXuatController() {
		super(new ChiTietNhapXuatDao());
		// TODO Auto-generated constructor stub
	}
	public List<ChiTietNhapXuat> findHDNgayTX(Date ngayThucXuat){
		boolean isTransacted = HibernateUtil.getCurrentSession()
				.getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession()
				: HibernateUtil.startCurrentSession();
		List<ChiTietNhapXuat> listChiTietNhapXuat=null;
		try {
			Query query = session
					.createQuery(
							"SELECT c FROM ChiTietNhapXuat c WHERE c.ngayThucXuat = :ngayThucXuat ").setDate("ngayThucXuat", ngayThucXuat);
			listChiTietNhapXuat= query.list();
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return listChiTietNhapXuat;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}
	}
}
